package com.yn.xr.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
private static Calendar cal = Calendar.getInstance();
	
	/**
	 * 获取两个时间相差的小时数 
	 * @param start
	 * @param end
	 * @return
	 */
	public static double getDiffHour(Date start ,Date end) {
		double diff = (end.getTime() - start.getTime())/1000/60/60;
		return diff;
	}
	
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	public static SimpleDateFormat sdf3 = new SimpleDateFormat("yyMMdd");
	public static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat sdf5 = new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat sdf6 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    public static SimpleDateFormat sdf7 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
    public static SimpleDateFormat sdf8 = new SimpleDateFormat("yyyy年MM月dd日");
    public static SimpleDateFormat sdf9 = new SimpleDateFormat("MM.dd");
    public static SimpleDateFormat sdf10 = new SimpleDateFormat("yyMMddHHmmss");
    
	public static Date toDataYYYYMMDD(String str) {
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date toDataYYYYMMDD(String str, SimpleDateFormat format) {
		try {
			return format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date toDataYYYYMMDDHHmmss(String str) {
		try {
			return sdf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String formatDataYYYYMMDD(Date date) {
		return sdf2.format(date);
	}
	public static String formatDataYYMMDD(Date date) {
		return sdf3.format(date);
	}
	public static String formatDataYYMMDDHHMMSS(Date date) {
		return sdf5.format(date);
	}
	public static String formatDatayyMMDDHHMMSS(Date date) {
		return sdf10.format(date);
	}
	
	public static String formatDataYYYYMMDDLine(Date date) {
		return sdf.format(date);
	}
	public static String formatDataToDatetime(Date date){
		return sdf4.format(date);
	}
	/**
	 * 获取当前日期month个月后的日期 
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date getAddMonthDate(Date date ,int month) {
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
	}
	 
	/**
	 * 格式化日期x年x月x日 xx:xx
	 * @param date
	 * @return
	 */
	public static String getZHDate(Date date) {
        String zhDate = sdf6.format(date);
        return zhDate;
	}
	public static String getZHShortDate(Date date){
		String zhShortDate = sdf8.format(date);
		return zhShortDate;
	}
	/**
     * 按天借款的方式 返款时间的计算
     * @param releaseTime 放款时间
     * @param termCount 借款天数
     * @return 还款日期
     */
    public static Date getDayRepayDate(Date releaseTime,int termCount) {
    	Calendar calendar = Calendar.getInstance();
        calendar.setTime(releaseTime);
        calendar.add(Calendar.DATE, termCount);
        return calendar.getTime();
    }
	/**
	 * 获取某天之前或者之后自然日 返回格式   ：yyyy-MM-dd
	 * @param releaseTime
	 * @param termCount   负数则为之前    正数则为之后
	 * @return
	 */
	public static String getNatureDay(Date releaseTime,int termCount){
		Date date = getDayRepayDate(releaseTime,termCount);
		return sdf.format(date);
	}
	/**
	 *  获取某天之前或者之后自然日 返回格式   ：yyyy-MM-dd HH:mm:ss
	 * @param releaseTime
	 * @param termCount 负数则为之前    正数则为之后
	 * @return String
	 * @Description:
	 * @author 刘恒   
	 * @date 2015年3月11日 下午12:47:18 
	 * @version V1.0
	 */
	public static Date getNatureDay(String releaseTime,int termCount){
        Calendar calendar = Calendar.getInstance() ;  
        try {  
            calendar.setTime(sdf4.parse(releaseTime)) ;  
        } catch (java.text.ParseException e) {  
            e.printStackTrace();  
        }  
        calendar.add(calendar.DATE,termCount) ;  
        return calendar.getTime() ;  
	}
	 
	/**
	 * 获取某天之前或者之后的几个月自然日 返回格式   ：yyyy-MM-dd
	 * @param date
	 * @param month   负数则为之前    正数则为之后
	 * @return
	 */
	public static String getNatureMonth(Date date ,int month){
		Date dateCal = getAddMonthDate(date,month);
		return sdf.format(dateCal);
	}
	
	/**
	 * 判断两个其实是否为同一天
	 * @param date
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date, Date date2){
		if(date == null || date2 == null) return false;
		return differDays(date, date2) == 0?true:false;
	}
	
	/**
	 *  判断参数日期是否为当天
	 * @param date
	 * @param todayDate
	 * @return
	 */
	public static boolean isToday(Date date){
		if(date == null) return false;
		return differDays(date, new Date()) == 0? true: false;
	}
	
	
	/**
	 * 判断给定日期与当天相差的天数     给定的日期为以后的日期则为负数
	 * @param date
	 * @param todayDate
	 * @return
	 */
	public static int differDays(Date date, Date baseDate){
		if(date == null) return -1;
		cal.setTime(date);
		Calendar today = Calendar.getInstance();
		today.setTime(baseDate);
		int differYearDay = today.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR);
		if(today.get(Calendar.YEAR) > cal.get(Calendar.YEAR)){
			int templYear = cal.get(Calendar.YEAR);
			differYearDay = 0;
			while(templYear != today.get(Calendar.YEAR)){
				cal.set(Calendar.YEAR, templYear ++);
				differYearDay += cal.getActualMaximum(Calendar.DAY_OF_YEAR);
			}
			differYearDay = differYearDay + today.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR);
		}else if(today.get(Calendar.YEAR) < cal.get(Calendar.YEAR)){
			int templYear = cal.get(Calendar.YEAR);
			differYearDay = cal.get(Calendar.DAY_OF_YEAR);
			templYear --;
			while(templYear != today.get(Calendar.YEAR)){
				cal.set(Calendar.YEAR, templYear --);
				differYearDay += cal.getActualMaximum(Calendar.DAY_OF_YEAR);
			}
			differYearDay += today.getActualMaximum(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
			differYearDay = -differYearDay;
		}
		return differYearDay;
	}
	
	/**
	 * 获取给定日期与当前日期相差天数
	 * @function:  
	 * @param date
	 * @return int   
	 * @exception 
	 * @author:HX1401122   
	 * @since  1.0.0
	 */
	public static int differDays(Date date){
		Date baseDate = new Date();
		return differDays(baseDate,date);
	}
	
	/**
	 * 
	 * @function:计算两个日期相差天数  
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @return int   
	 * @exception 
	 * @author:肖震   
	 * @since  1.0.0
	 */
	public static int differDaysWithoutRigor(Date startDate, Date endDate){
		
		try
		{
			return daysBetween(startDate,endDate);
		}
		catch (ParseException e)
		{
			e.printStackTrace();  
		}
		return 0;
	}
	
	
	public static Date nextYear(int hav, Date date){
		return next(Calendar.YEAR, hav, date);
	}
	
	public static Date nextMonth(int hav, Date date){
		return next(Calendar.MONTH, hav, date);
	}
	
	public static Date nextDay(int hav, Date date){
		return next(Calendar.DAY_OF_MONTH, hav, date);
	}
	
	public static Date next(int dateType, int hav, Date date){
		cal.setTime(date);
		cal.add(dateType, hav);
		return cal.getTime();
	}
	
	public static Date nextDayUpdateTime(int hav, Date date){
		Calendar calOrg = Calendar.getInstance();
		calOrg.setTime(date);
		Calendar newCal = Calendar.getInstance();
		newCal.set(Calendar.YEAR, calOrg.get(Calendar.YEAR));
		newCal.set(Calendar.MONTH, calOrg.get(Calendar.MONTH));
		newCal.set(Calendar.DAY_OF_MONTH, calOrg.get(Calendar.DAY_OF_MONTH));
		newCal.add(Calendar.DAY_OF_MONTH, hav);
		return newCal.getTime();
	}
	
	public static String nextDayByHour(int hav, Date date){
    	SimpleDateFormat dft=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
    	Calendar   dar=Calendar.getInstance();     
    	dar.setTime(date);     
    	dar.add(java.util.Calendar.HOUR_OF_DAY, hav);     
        return dft.format(dar.getTime());
	}
	/**
	 * 比较给的时间与现在的时间前后顺序   true:之后的时间、 false:之前的时间
	 * @param hour
	 * @param minute
	 * @return
	 */
	public static boolean afterTime(int hour, int minute){
		int nowHour = cal.get(Calendar.HOUR_OF_DAY);
		int nowMinute = cal.get(Calendar.MINUTE) + 5;
		
		
		if(nowHour < hour) return true;
		if(nowHour > hour) return false;
		if(nowHour == hour && nowMinute <= minute) return false;
		return true;
	}
	/**
	 * 
	 * description: 将日期转字符串
	 * 
	 * @param date 日期
	 * @param format 日期格式
	 * @return String 字符串
	 * @author:肖震
	 * @since 1.0.0
	 */
	public static String dateToString(Date date, String format)
	{
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}
	/**
	 * 
	 * @function:字符串转日期  
	 * @param str 字符串
	 * @param formatt 日期格式，如：yyyy-MM-dd
	 * @return Date   
	 * @exception 
	 * @author:肖震   
	 * @since  1.0.0
	 */
	public static Date StringToDate(String str,String formatt)
	{
		Date date = null;
		try {
			  if(!StringUtils.isEmpty(str))
			  {
		        SimpleDateFormat format = new SimpleDateFormat(formatt);		   
		   
		         date = format.parse(str);
			   }
		   } catch (ParseException e) {
		    e.printStackTrace();
		   }
		   return date;
	}
	/**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate) throws ParseException    
    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        smdate=sdf.parse(sdf.format(smdate));  
        bdate=sdf.parse(sdf.format(bdate));  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }    
      
    /**
     * 
     * @function:字符串的日期格式的计算  
     * @param smdate
     * @param bdate
     * @return int
     * @throws ParseException    
     * @exception 
     * @author:肖震   
     * @since  1.0.0
     */
    public static int daysBetween(String smdate,String bdate) throws ParseException{  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(smdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(bdate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));     
    }  
    
    /**
     * @function: unix时间戳转换为日期  
     * @param timeStamp
     * @return
     * @throws ParseException Date   
     * @exception 
     * @author:duyubo   
     * @since  1.0.0
     */
    public static Date timeStampToDate(Long unixTimeStamp) throws ParseException{
    	if(unixTimeStamp != null){
    		SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
    		return format.parse(format.format(unixTimeStamp*1000));
    	}
    	return null;
    }
    
    /**
     * @function:秒数转为时间格式  
     * @param time
     * @return String   
     * @exception 
     * @author:duyubo   
     * @since  1.0.0
     */
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time > 0){
        	minute = time / 60;
        	if (minute < 1){
        		timeStr = time +"秒";
        	} else if (minute < 60) {
        		second = time % 60;
        		timeStr = minute + "分" + unitFormat(second) +"秒";
        	} else {
        		hour = minute / 60;
        		if (hour > 99)
        			return "99小时59分59秒";
        		minute = minute % 60;
        		second = time - hour * 3600 - minute * 60;
        		timeStr = hour + "小时" + unitFormat(minute) + "分" + unitFormat(second) +"秒";
        	}
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }
    /**
     * 计算两个时间的秒差
     * @param start
     * @param end
     * @return int
     * @Description:
     * @author 刘恒   
     * @date 2015年3月14日 下午10:42:06 
     * @version V1.0
     */
    public static int differSecond(Date start ,Date end){
    	return (int)((end.getTime()-start.getTime())/1000);
    }

    
    /**
     * 
     * @function:   根据数字以及类型获取相差之后的日期
     * @param date
     * @param number
     * @param dateType
     * @return Date   
     * @exception 
     * @author:黄伟  
     * @since  1.0.0
     */
    public static Date getAddDate(Date date ,int number,String dateType) {
    	Calendar calendar = Calendar.getInstance();
    	if("month".equals(dateType)){
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, number);
    	}else if("day".equals(dateType)){
    		calendar.setTime(date);
            calendar.add(Calendar.DATE, number);
    	}else if("year".equals(dateType)){
    		calendar.setTime(date);
            calendar.add(Calendar.YEAR, number);
    	}
    	return calendar.getTime();
		
	}
    

    /**
	 * 
	 * @function:字符串转日期  
	 * @param str 字符串
	 * @return Date   
	 * @exception 
	 * @author:dengli   
	 * @since  1.0.0
	 */
	public static Date StringToDate(String str)
	{
		Date date = null;
		try {
			  if(!StringUtils.isEmpty(str))
			  {
		         date = sdf5.parse(str);
			   }
		   } catch (ParseException e) {
		    e.printStackTrace();
		   }
		   return date;
	}
	
	/**
	 * 将时间减去指定的毫秒数
	 * @param startTime
	 * @param number
	 * @return
	 * @see [类、类#方法、类#成员]
	 * @param
	 * @author lifang
	 * @version 1.0.0
	 * @data 2015年6月15日
	 */
	public static Date secondDate(Date startTime, Integer number)
	{
	    Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.SECOND, - number);
        return calendar.getTime();
	}
	
	public static void main(String[] args) throws ParseException {
		try
		{
			java.sql.Date startDate= new java.sql.Date(DateUtil.nextDay(-15, new Date()).getTime());
			System.out.println(sdf6.format(startDate));
			System.out.println("unitTimeToDate:"+timeStampToDate((long)1425612091));
			
			System.out.println("秒数转换小时：  "+secToTime(111112));
		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println("mmmmmmmmmmmm:"+new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format(getAddDate(new Date(),10,"day")));
//		System.out.println("ddddddddddddddddd:"+new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format(getAddDate(new Date(),10,"month")));
//		System.out.println("yyyyyyyyyyyyy:"+new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format(getAddDate(new Date(),10,"year")));
//		System.out.println("想拆aaaaaaaaaaaaaaaaaaaa:"+differDays(new Date(),getAddDate(new Date(),1,"year")));
//		System.out.println(getZHDate(new Date()));
//		System.out.println(sdf.format(getDayRepayDate(new Date(), 33)));
//		System.out.println(getNatureDay(new Date(), -3));
//		System.out.println(getNatureMonth(new Date(), 1));
//		System.out.println(sdf4.format(new Date()));
//		System.out.println(DateUtil.getZHShortDate(new Date()));
//		
//		try {
//			Date oldDate = DateUtil.sdf4.parse("2014-07-30 00:00:01");
//			System.out.println(DateUtil.sdf4.format(oldDate));
//			System.out.println(DateUtil.sdf4.format(DateUtil.nextDayUpdateTime(1, oldDate)));
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//		
//		String str = DateUtil.sdf9.format(DateUtil.nextDay(1, new Date()));
//		System.out.println(Integer.parseInt(str.substring(0,str.indexOf("."))));
//		System.out.println(Integer.parseInt(str.substring(str.indexOf(".")+1)));
//		System.out.println(DateUtil.getDiffHour(new Date(), new Date()));
//		System.out.println(nextDayByHour(26,new Date()));
//		System.out.println(nextDayByHour(1,new Date()));
		Date start = sdf4.parse(DateUtil.nextDayByHour(2, new Date()));
		Date end = sdf4.parse(DateUtil.nextDayByHour(2, new Date()));
		Date start1 = sdf4.parse("2014-07-30 02:00:01");
		Date end1 = sdf4.parse("2014-07-30 02:10:01");
		end1.getTime();
		Calendar end1calendar = Calendar.getInstance();
		end1calendar.setTime(end1);
        Calendar start1calendar = Calendar.getInstance();
        start1calendar.setTime(start1);
		System.out.println(differSecond(start1,end1));

	}
}
